package org.jdu.dao;


import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jdu.exception.ServiceException;
import org.jlu.logger.PrdLoggerFactory;

public class DAOControllerService extends DAOService {

	public static final String COLUMN_SIZE = "COLUMN_SIZE";

	public static final String DATA_TYPE = "DATA_TYPE";

	public static final String NULLABLE = "NULLABLE";
	
	private transient static Logger log = PrdLoggerFactory.getInstance().getLogger(
			"crparch");

	public static  int maxFieldLength(String dbName, String schemaName,
			String tableName, String columnName) throws ServiceException {
		DAOService dao = new DAOService();
		DBConnection db = dao.getDbConnection();
		Connection con = null;
		ResultSet rs = null;
		try {
			con = db.getConnection(dbName);
			DatabaseMetaData dbmd = con.getMetaData();
			rs = dbmd.getColumns(null, schemaName, tableName,
					columnName);
			if (rs.next()) {
				int length = rs.getInt("COLUMN_SIZE");
				return length;
			}
			
		} catch (DbException e) {
			log.error("[DAOControllerService::maxFieldLength]  "
					+ e.getMessage(), e);
			throw new ServiceException(e.getMessage());
		} catch (SQLException e) {
			log.error("[DAOControllerService::maxFieldLength]  "
					+ e.getMessage(), e);
			throw new ServiceException(e.getMessage());
		}
		finally{
			log.info("[DAOControllerService::maxFieldLength] END");
			dao.closeConnection(con, null, rs);
		}
		return 0;
	}
	
	/**
	 * Carica i metadata del db
	 * @param dbKeyName 
	 * @param catalog 
	 * @param schemaPattern 
	 * @param tableNamePattern 
	 * @param columnNamePattern 
	 * @return
	 * @throws ServiceException 
	 */
	public static Map loadDbInfo(String dbKeyName, String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws ServiceException{
		DAOService dao = new DAOService();
		DBConnection db = dao.getDbConnection();
		Connection con = null;
		ResultSet rs = null;
		Map map = new HashMap();
		try{
			con = db.getConnection(dbKeyName);
			DatabaseMetaData dbmd = con.getMetaData();
			rs = dbmd.getColumns(catalog, schemaPattern, tableNamePattern, columnNamePattern);
			ResultSetMetaData rsmd = rs.getMetaData();
			while(rs.next()){
				String tableName = rs.getString("TABLE_NAME");
				Map columns = (Map) map.get(tableName);
				if(columns==null){
					columns=new HashMap();
				}
				String columnName = rs.getString("COLUMN_NAME");
				Map columnInfo = (Map) columns.get(columnName);
				if(columnInfo == null ){
					columnInfo = new HashMap();
				}
				columnInfo.put(COLUMN_SIZE, rs.getString("COLUMN_SIZE"));
				columnInfo.put(DATA_TYPE, rs.getString("DATA_TYPE"));
				columnInfo.put(NULLABLE, rs.getString("NULLABLE"));
				columns.put(columnName, columnInfo);
				
				map.put(tableName, columns);
			}
			return map;
		}
		catch(Exception e){
			log.error("[DAOControllerService::loadDbInfo]  "
					+ e.getMessage(), e);
			throw new ServiceException(e.getMessage());
		}finally{
			dao.closeConnection(con, null, rs);
		}
		
	
	}
}
